package com.bite.blog.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Slf4j
public class SecurityUtils {
    public static String encrypt(String password){
        String salt = UUID.randomUUID().toString().replace("-","");
        String securityPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return salt+securityPassword;
    }
    public static boolean verify(String inputPassword,String sqlPassword){
        if(sqlPassword==null||sqlPassword.length()!=64){
            log.error("数据库的密码格式不对");
            return false;
        }
        String salt = sqlPassword.substring(0,32);
        String secretPassword = DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes());
        return sqlPassword.equals(salt+secretPassword);
    }
}